<?php
class LilSidebar {
	private $request;
	
	public function __construct($controller) {
		$this->request = $controller->request;
		
		// set current user
		App::uses('LilCurrentUser', 'Lil.Lib');
		
		$this->currentUser = LilCurrentUser::getInstance();
	}
	
	public function build() {
		return array(
			'welcome' => array(
				'visible' => true,
				'title' => __d('lil', 'Welcome'),
				'active' => 
					($this->request->params['plugin'] == 'lil') &&
					(
					($this->request->params['controller'] == 'areas' &&
					(in_array($this->request->params['action'], array('admin_dashboard', 'admin_select', 'admin_list'))))
					||
					($this->request->params['controller'] == 'users' &&
					(in_array($this->request->params['action'], array('admin_properties'))))
					),
				'url' => array(
					'plugin' => 'lil',
					'controller' => 'areas',
					'admin' => true,
					'action' => 'dashboard'
				),
				'items'   => array(
					'dashboard' => array(
						'visible' => true,
						'title' => sprintf(__d('lil', 'Welcome, %s'), $this->currentUser->get(Configure::read('Lil.userDisplayField'))),
						'url'   => array(
							'plugin'     => 'lil',
							'controller' => 'areas',
							'admin'      => true,
							'action'     => 'dashboard',
						),
						'params' => array(),
						'active' => false,
						'expand' => 
							($this->request->params['controller'] == 'areas' && 
							in_array($this->request->params['action'], array('admin_dashboard', 'admin_list')))
							||
							($this->request->params['controller'] == 'users' && 
							in_array($this->request->params['action'], array('admin_properties')))
							,
						'submenu' => array(
							'dashboard' => array(
								'visible' => true,
								'title' => __d('lil', 'Dashboard', true),
								'url'   => array(
									'plugin'     => 'lil',
									'controller' => 'areas',
									'action'     => 'dashboard',
									'admin'      => true,
								),
								'params' => array(),
								'active' =>
									$this->request->params['controller'] == 'areas' &&
									$this->request->params['action'] == 'admin_dashboard'
							),
							'area' => array(
								'visible' => Configure::read('Lil.noAreas') !== true,
								'title' => __d('lil', 'Change admin area', true),
								'url'   => array(
									'plugin'     => 'lil',
									'controller' => 'areas',
									'action'     => 'list',
									'admin'      => true,
								),
								'params' => array(),
								'active' =>
									$this->request->params['controller'] == 'areas' &&
									$this->request->params['action'] == 'admin_list'
							),
							'properties' => array(
								'visible' => true,
								'title' => __d('lil', 'Edit user profile', true),
								'url'   => array(
									'plugin'     => 'lil',
									'controller' => 'users',
									'action'     => 'admin_properties',
									'admin'      => true,
								),
								'params' => array(),
								'active' =>
									$this->request->params['controller'] == 'users' &&
									$this->request->params['action'] == 'admin_properties'
							),
							'logout' => array(
								'visible' => true,
								'title' => __d('lil', 'Logout', true),
								'url'   => array(
									'plugin'     => 'lil',
									'controller' => 'users',
									'action'     => 'logout',
									'admin'      => false,
								),
								'params' => array(),
								'active' => false
							),
						)
					)
				)
			),
			'admin' => array(
				'visible' => (Configure::read('Lil.allowUsersAnything') || $this->currentUser->role('admin')),
				'title' => __d('lil', 'Settings', true),
				'active' =>
					($this->request->params['plugin']=='lil') &&
					in_array($this->request->params['controller'], array('areas', 'users')) &&
					in_array($this->request->params['action'], array('admin_index', 'admin_edit', 'admin_add')),
				'url' => array('admin' => true, 'plugin' => 'lil', 'controller' => 'users', 'action' => 'admin_index'),
				'items' => array(
					'users' => array(
						'visible' => true,
						'title' => __d('lil', 'Users', true),
						'url'   => array(
							'plugin'     => 'lil',
							'controller' => 'users',
							'action'     => 'index',
							'admin'      => true,
						),
						'params' => array(),
						'active' => in_array($this->request->params['controller'], array('users')) &&
							in_array($this->request->params['action'], array('admin_index', 'admin_edit', 'admin_add')),
						'expand' => false,
						'submenu' => array()
					),
					'areas' => array(
						'visible' => Configure::read('Lil.noAreas') !== true,
						'title' => __d('lil', 'Areas', true),
						'url'   => array(
							'admin'      => true,
							'plugin'     => 'lil',
							'controller' => 'areas',
							'action'     => 'index',
						),
						'params' => array(),
						'active' => in_array($this->request->params['controller'], array('areas')) &&
							in_array($this->request->params['action'], array('admin_index', 'admin_edit', 'admin_add')),
						'expand' => false,
						'submenu' => array()
					)
				) // end if items
			),
		);
	}
}